package com.k.demo.controller;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.k.demo.entity.Notice;
import com.k.demo.service.NoticeService;

@RestController
@RequestMapping("/notice")
public class NoticeController {

	@Autowired
	private NoticeService noticeService;
	
	@Value("${lc.upload.path}")
	private String uploadPath;
	
	/**
	 * 返回全部公告
	 * url /notice/all
	 * @author zzg
	 */
	@RequestMapping("/all")
	public Object all() {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("noticeList", noticeService.getAll());
		
		return map;
	}
	
	
	/**
	 * 返回当前显示的公告 状态为1
	 * url /notice/shownotice
	 * @author zzg
	 */
	@RequestMapping("/shownotice")
	public Object showNotice() {
		
		return noticeService.showNotice();
		
	}
	
	
	/**
	 * 公告管理时的分页展示
	 * url/all/page
	 * @author shy
	 */
	@RequestMapping("/all/page")
	public Object allpage(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("foodList", noticeService.getpage((int)map.get("pageNum"),(int)map.get("pageSize")));
		
		return rmap;
	}
	
	/**
	 * 公告管理的删除
	 * url/all/page
	 * @author shy
	 */
	@RequestMapping("/delete")
	public Object deletById(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("delete", noticeService.deleteById((int)map.get("noticeid")));
		
		return rmap;
		
	}
	
	/**
	 * 公告管理的增加  这个地方涉及图片的上传，最后在写
	 * url/all/page
	 * @author shy
	 */
	@RequestMapping("/add")
	public Object addNotice(@RequestParam("notice_content") String notice_content,@RequestParam MultipartFile[] mpfs, HttpServletRequest request) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		//设置图片路径
		String saveFileName = UUID.randomUUID().toString().replaceAll("-", "") + "_" + mpfs[0].getOriginalFilename();
		String filePath = uploadPath + "\\" + saveFileName;
		System.out.println(filePath);
		
		File f = new File(filePath);
		System.out.println(f.exists());
			
		try {
			mpfs[0].transferTo(f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//获取系统时间
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
	    String nowtime = df.format(date);
		
		Notice notice = new Notice();
		notice.setNotice_content(notice_content);
		notice.setNotice_time(nowtime);
		notice.setNotice_img(saveFileName);
		notice.setNotice_status(0);
		notice.setFoperate_flag(1);
		
		map.put("addResult", noticeService.addNotice(notice));
		
		
//		rmap.put("delete", noticeService.deleteById((int)map.get("noticeid")));
		
		return map;
		
	}
	
	/**
	 * 公告管理的修改  这个地方涉及图片的上传，最后在写
	 * url/all/page
	 * @author shy
	 */
	@RequestMapping("/modify")
	public Object modifyNotice(@RequestParam("notice_id") Integer notice_id,@RequestParam("notice_content") String notice_content,@RequestParam MultipartFile[] mpfs) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		//设置图片路径
		String saveFileName = UUID.randomUUID().toString().replaceAll("-", "") + "_" + mpfs[0].getOriginalFilename();
		String filePath = uploadPath + "\\" + saveFileName;
		System.out.println(filePath);
		
		File f = new File(filePath);
		System.out.println(f.exists());
			
		try {
			mpfs[0].transferTo(f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Notice notice = new Notice();
		notice.setNotice_id(notice_id);
		notice.setNotice_content(notice_content);
		notice.setNotice_time(null);
		notice.setNotice_img(saveFileName);
		notice.setNotice_status(0);
		notice.setFoperate_flag(1);
		
		map.put("modifyResult", noticeService.modifyNotice(notice));
		
		return map;
	}
	
	/**
	 * 公告管理的模糊查询(公告内容的检索)
	 * url/all/page
	 * @author shy
	 */
	@RequestMapping("/searchByContent")
	public Object searchNoticeByName(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		
		rmap.put("searchByNameList", noticeService.getpagebyname((int)map.get("pageNum"),(int)map.get("pageSize"),(String)map.get("notice_content")));
		
		return rmap;
		
	}
	

	/**
	 * 公告管理的时间间区间(公告内容的检索)
	 * url/all/page
	 * @author shy
	 */
	@RequestMapping("/searchByTime")
	public Object searchNoticeByTime(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("searchByNameList", noticeService.getpagebytime((int)map.get("pageNum"),(int)map.get("pageSize"),(String)map.get("time1"),(String)map.get("time2")));
		
		return rmap;
		
	}
	

	/**
	 * 公告撤销
	 * url/notice/revokeNotice
	 * @author shy
	 */
	@RequestMapping("/revokeNotice")
	public Object revokeNotice(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("revokeResult", noticeService.revokeNotice((Integer)map.get("notice_id")));
		
		return rmap;
		
	}
	
	/**
	 * 公告修改状态
	 * url/notice/modifystatus
	 * @author shy
	 */
	@RequestMapping("/modifystatus")
	public Object modifyStatus(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		//这一行删除
		noticeService.modifyAllStatus();
		
		rmap.put("modifyResult",noticeService.modifyState((int)map.get("notice_id")));
		
		return rmap;
	}
	
	/**
	 * 公告批量删除
	 * url/notice/deletegroup
	 * @author shy
	 */
	@RequestMapping("/deletegroup")
	public Object deleteNotices(@RequestBody String deleteGroup) {
		
		JSONObject jsonObject = JSON.parseObject(deleteGroup);
		
		List<Integer> deleteList = JSON.parseArray(jsonObject.getString("deleteList"), Integer.class);
		
		System.out.println(deleteList.isEmpty());
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("deleteGroupResult", noticeService.deleteByGroup(deleteList));
		
		return map;
	}

	
}
